package ilender.typemappers;

import java.sql.*;

import org.apache.ibatis.type.*;
import org.joda.time.DateTime;

import ilender.util.*;

/**Used for converting JodaTime's datetime to PostgreSQL date and vice versa.*/
public class JodaDateTimeHandler extends BaseTypeHandler<DateTime>{

	public void setNonNullParameter(PreparedStatement ps,int i,DateTime parameter,JdbcType jdbcType) throws SQLException{
		ps.setTimestamp(i,new Timestamp(parameter.getMillis()));
	}

	public DateTime getNullableResult(ResultSet rs, String columnName) throws SQLException{
		Timestamp timestamp=rs.getTimestamp(columnName);
		if(null!=timestamp)
			return new DateTime(timestamp.getTime());
		else
			return null;
	}

	public DateTime getNullableResult(ResultSet rs, int columnIndex) throws SQLException{
		Timestamp timestamp=rs.getTimestamp(columnIndex);
		if(null!=timestamp)
			return new DateTime(timestamp.getTime());
		else
			return null;
	}

	public DateTime getNullableResult(CallableStatement cs, int columnIndex) throws SQLException{
		Timestamp timestamp=cs.getTimestamp(columnIndex);
		if(null!=timestamp)
			return new DateTime(timestamp.getTime());
		else
			return null;
	}
}
